/***************************************************************************
*   Copyright (C) 2011 by Chuck Gao & Alex Liu                             *
*                                                                          *
*   This program is free software: you can redistribute it and/or modify   *
*   it under the terms of the GNU General Public License as published by   *
*   the Free Software Foundation, either version 3 of the License, or      *
*   (at your option) any later version.                                    *
*                                                                          *
*   This program is distributed in the hope that it will be useful,        *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
*   GNU General Public License for more details.                           *
*                                                                          *
*   You should have received a copy of the GNU General Public License      *
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
*                                                                          *
*   Contact author: chuckgao.cg@gmail.com                                  *
*                   alex2202375@gmail.com                                  *
***************************************************************************/

import QtQuick 1.0
import "../Script/createPlanDayView.js" as PlanDayViewCreaetor

Item {
    id: myCalendar
    width: 360
    height: 640
    clip: true

    property date currentDate: new Date()
    onCurrentDateChanged: reload()

    property Component headerRenderer: myCalendarWidgets.headerRenderer
    property Component daysRenderer: myCalendarWidgets.daysRenderer

    function showDayView()
    {
        PlanDayViewCreaetor.createPlanSubView()
    }

    function reload() {
        monthModel.clear()

        var tempDate = new Date(currentDate)
        tempDate.setDate(1)

        var dayOfWeek = tempDate.getDay()
        for (var i = 0; i < dayOfWeek; i++)
            monthModel.append({"day": "", "current": false})

        var month = tempDate.getMonth()
        for (var dayCount = 1; dayCount <= 31 ; dayCount++) {
            tempDate.setDate(dayCount)
            if (tempDate.getMonth() != month)
                break

            var current = false
            if (dayCount == currentDate.getDate())
                current = true

            monthModel.append({"day": dayCount, "current": current})
        }

        // Fill the remaining slots
        for (var i = dayCount + dayOfWeek; i < 43; i++)
            monthModel.append({"day": "", "current": false})
    }

    function nextMonth() {
        var newDate = new Date(currentDate)

        if (currentDate.getMonth() == 11) {
            newDate.setMonth(0)
            newDate.setFullYear(currentDate.getFullYear() + 1)
        } else
            newDate.setMonth(currentDate.getMonth() + 1)

        currentDate = newDate
    }

    function previousMonth() {
        var newDate = new Date(currentDate)

        if (currentDate.getMonth() == 0) {
            newDate.setMonth(11)
            newDate.setFullYear(currentDate.getFullYear() - 1)
        } else
            newDate.setMonth(currentDate.getMonth() - 1)

        currentDate = newDate
    }

    // Implementation

    ListModel {
        id: monthModel

        // day: int (the day of the month)
        // current: bool (is the current day)
    }

    Rectangle {
        id: calendarTitle
        width: parent.width
        height: 60
        color: "orange"
        opacity: 0.8

        Image {
            id: prev
            width: 30
            height: 30
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.verticalCenter: parent.verticalCenter
            smooth: true
            source: "qrc:/plan/Images/Plan/previous1.png"

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    previousMonth()
                }
            }
        }

        Text {
            anchors.centerIn: parent
            text: "June 27, 2011"
        }

        Image {
            id: next
            width: 30
            height: 30
            anchors.right: parent.right
            anchors.rightMargin: 10
            anchors.verticalCenter: parent.verticalCenter
            smooth: true
            source: "qrc:/plan/Images/Plan/next1.png"

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    nextMonth()
                }
            }
        }
    }

    Row {
        id: dayOfWeekLabel
        anchors.top: calendarTitle.bottom
        anchors.left: parent.left
        Repeater {
            model: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

            delegate: Item {
                id: headerDelegate

                property int theIndex: index // XXX: See ChoiceList.qml
                property variant theModel: model

                width: myCalendar.width / 7
                height: 30

                Loader {
                    property alias model: headerDelegate.theModel
                    property alias index: headerDelegate.theIndex
                    property alias cellWidth: headerDelegate.width
                    property alias cellHeight: headerDelegate.height

                    anchors.fill: parent

                    sourceComponent: headerRenderer
                }
            }
        }
    }

    Grid {
        id: grid
        anchors.top: dayOfWeekLabel.bottom
        anchors.left: parent.left

        columns: 7
        rows: 6

        Repeater {
            model: monthModel

            delegate: Item {
                id: daysDelegate

                property int theIndex: index // XXX: See ChoiceList.qml
                property variant theModel: model

                width: myCalendar.width / grid.columns
                height: 60

                Loader {
                    property alias model: daysDelegate.theModel
                    property alias index: daysDelegate.theIndex
                    property alias cellWidth: daysDelegate.width
                    property alias cellHeight: daysDelegate.height

                    anchors.fill: parent

                    sourceComponent: daysRenderer
                }

            }
        }
    }

    CalendarWidgets { id: myCalendarWidgets }
}
